package com.bw.risk.ground;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bw.core.creditStatus.DeleteStatus;
import com.bw.core.creditStatus.RiskStatus;
import com.bw.core.vo.OrderRiskVO;
import com.bw.core.vo.RiskOrderVO;
import com.bw.risk.service.TbRiskOrderService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import com.bw.risk.mapper.TbRiskOrderMapper;
import com.bw.risk.pojo.TbRiskOrder;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Configuration
@RabbitListener(queues = "item_queue")
@EnableRabbit
@Component
public class CodeGround {

    @Autowired
    private TbRiskOrderMapper riskOrderMapper;

    @Autowired
    private TbRiskOrderService riskOrderService;

    @Autowired
    private Snowflake snowflake;//雪花算法


    @RabbitHandler
    public void getMsg(String msg) {
        JSONObject jsonObject = JSONObject.parseObject(msg);
        OrderRiskVO orderRiskVO = jsonObject.toJavaObject(jsonObject, OrderRiskVO.class);
        if (ObjectUtil.isEmpty(orderRiskVO)) {
            return;
        }
        QueryWrapper<TbRiskOrder> riskOrderQueryWrapper = Wrappers.query();
        riskOrderQueryWrapper.eq("order_no",orderRiskVO.getOrderNo());
        List<TbRiskOrder> riskOrders = riskOrderMapper.selectList(riskOrderQueryWrapper);
        if (!CollectionUtils.isEmpty(riskOrders)) {  //tbRiskOrders是否为空
            return;
        }
        TbRiskOrder riskOrder = new TbRiskOrder();
        riskOrder.setRiskNo(snowflake.nextId() + "");
        riskOrder.setOrderNo(orderRiskVO.getOrderNo());
        riskOrder.setStatus(orderRiskVO.getOrderStatus());
        riskOrder.setUserId(orderRiskVO.getUserId());
        riskOrder.setUserIdentityNo(orderRiskVO.getUserIdentityNo());
        riskOrder.setUserPhone(orderRiskVO.getUserPhone());
        riskOrder.setUserName(orderRiskVO.getUserName());
        riskOrder.setOrderApplyMoney(orderRiskVO.getOrderApplyMoney());
        riskOrder.setRiskMsg("暂无说明");
        riskOrder.setStatus(0);//待审核
        riskOrder.setIsDelete(DeleteStatus.NOT_DELETE);//未删除
        riskOrder.setCreateTime(new Date());
        riskOrder.setUpdateTime(new Date());
        riskOrderMapper.insert(riskOrder);

    }
}
